axios.defaults.baseURL = "http://ajax-api.itheima.net";
const myToast = new bootstrap.Toast(document.querySelector("#myToast"), {
  delay: 2000, // 显示多久后 自动消失
});
const toast = document.querySelector("#myToast .toast-body");
axios.interceptors.request.use(
  function (config) {
    // console.log(config);
    if (localStorage.getItem("token")) {
      config.headers.Authorization = localStorage.getItem("token");
    }
    return config;
  },
  function (error) {
    console.log(error);
    return Promise.reject(error);
  }
);

axios.interceptors.response.use(
  function (response) {
    // myToast.show();
    // toast.innerHTML=response.data.message
    // console.dir(response);
    return response;
  },
  function (error) {
    myToast.show();
    // console.dir(error);
    if (error.response.status==422) {
      toast.innerHTML=error.response.data.detail[0].message
    }else if (error.response.status==401) {
      toast.innerHTML=error.response.data.message
      location.href='/login.html'
    }
    else{
      toast.innerHTML=error.response.data.message
    }
    console.dir(error);
    return Promise.reject(error);
  }
);
